package com.hm.cms.websit;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hm.cms.service.ArticleService;
import com.hm.cms.service.TermsService;
import com.hm.cms.util.WebSitUtil;
import com.hm.cms.vo.HcmsArticleVo;
import com.hm.cms.vo.HcmsTermsVo;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.StringUtil;
import com.hm.common.util.WebUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年11月29日 下午5:09:15
 * @version 1.0
 * @describe 
 */
@Slf4j
@Component
public class JinseCaiJinHandler {
	
	@Autowired
	private TermsService termsService;

	@Autowired
	private ArticleService articleService;

	public void handler() {
		for(int page=3 ; page > 0;page--) {
			mainDirectory("5a1e8e91953f1f1cb0467aa2",MessageFormat.format("http://www.jinse.com/blockchain/page_{0}", page),"1");//区块链
			WebSitUtil.wating3();
		}
	}
	
	public static void main(String[] args) throws Exception{
		String uri = "http://www.jinse.com/news/blockchain/102812.html";
		Document document = Jsoup.connect(uri).ignoreContentType(true)
				.userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)").get();
		
		Elements elements = document.getElementsByClass("article");
		if(CommonUtil.isEmpty(elements)) {
			return ;
		}
		Element contentEl = elements.get(0);
		
		Elements titleEls = contentEl.getElementsByTag("h1");
		if(CommonUtil.isEmpty(titleEls)) {
			return ;
		}
		
		String title = titleEls.get(0).html();
		System.out.println("title------->"+title);

		Elements articleEls = contentEl.getElementsByClass("con");
		if(CommonUtil.isEmpty(titleEls)) {
			return ;
		}
		String content = articleEls.get(0).html();
		System.out.println("content------>"+content);
	}
	
	private void loadArticle(String uri,String imgUri,String termTaxonomy,String operationUserId) {
		try {
			if (articleService.checkArticleSourceUri(uri)) {
				return;
			}
//			String uri = "http://www.jinse.com/news/blockchain/102812.html";
			Document document = Jsoup.connect(uri).ignoreContentType(true)
					.userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)").get();
			
			Elements elements = document.getElementsByClass("article");
			if(CommonUtil.isEmpty(elements)) {
				return ;
			}
			Element contentEl = elements.get(0);
			
			Elements titleEls = contentEl.getElementsByTag("h1");
			if(CommonUtil.isEmpty(titleEls)) {
				return ;
			}
			
			String title = titleEls.get(0).html();
			System.out.println("title------->"+title);

			Elements articleEls = contentEl.getElementsByClass("con");
			if(CommonUtil.isEmpty(titleEls)) {
				return ;
			}
			String content = articleEls.get(0).html();
			System.out.println("content------>"+content);
			
			
			String description = document.select("meta[name=description]").get(0).attr("content");
			String keyStr = document.select("meta[name=keywords]").get(0).attr("content");
			System.out.println(keyStr);
			
			List<String> termIds = new ArrayList<String>();
			String[] keys = keyStr.split(",");
			for (String key : keys) {
				if(CommonUtil.isEmpty(key)) {
					continue;
				}
				HcmsTermsVo hcmsTermsVo = termsService.getHcmsTermsByTermName(key);
				if (CommonUtil.isNotEmpty(hcmsTermsVo)) {
					termIds.add(hcmsTermsVo.getId());
					continue;
				}

				HcmsTermsVo termsVo = new HcmsTermsVo();
				termsVo.setTermName(key);
				termsVo.setTermCode(key);
				termsVo.setTermOrder(-1);
				termIds.add(termsService.addTerms(termsVo, operationUserId));
			}
			
			if(StringUtil.isBlankOrNull(description)) {
            	description = StringUtil.dislodgeAllBlank(WebUtil.clearHtmlTags(content)).substring(0, 150);
            }
            
			HcmsArticleVo articleVo = new HcmsArticleVo();
			{
				articleVo.setTermIds(termIds);
				articleVo.setTermTayIds(Arrays.asList(termTaxonomy));
				articleVo.setArticleContent(content);
				articleVo.setBreviaryIcon(imgUri);
				articleVo.setAuthorName("宋小米");
				articleVo.setReleaseTime(System.currentTimeMillis());
				articleVo.setTitle(title);
				articleVo.setKeywords(StringUtil.isBlankOrNull(keyStr)?title:keyStr);
				articleVo.setSourceName("金色财经");
				articleVo.setSourceUri(uri);
				articleVo.setArticleOrder((int) articleService.getArtilcleMaxOrder());
				articleVo.setArticleExcerpt(description);
			}
			articleService.addArticle(articleVo, operationUserId);
			
		}catch(Exception e) {
			log.error(e.getMessage(), e);
		}
	}
	
	private void mainDirectory(String termTaxonomy,String uri,String operationUserId) {
		try {
//			String uri = "http://www.jinse.com/blockchain/page_2";
			Document document = Jsoup.connect(uri).ignoreContentType(true)
					.userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)").get();
			
			Elements elements = document.getElementsByClass("list");
			for (Element el : elements) {
				Elements olEls = el.getElementsByTag("ol");
				for (Element olEl : olEls) {
					
					String imgUrl = null;
					Elements imgEls = olEl.getElementsByTag("img");
					if(CommonUtil.isNotEmpty(imgEls)) {
						imgUrl = imgEls.get(0).attr("src");
					}
					Elements articleEls = olEl.getElementsByTag("h3");
					if(CommonUtil.isNotEmpty(articleEls)) {
						Elements articleUriEls = articleEls.get(0).getElementsByTag("a");
						if(CommonUtil.isNotEmpty(articleEls)) {
							String articleUrl = articleUriEls.get(0).attr("href");
							
							System.out.println(articleUrl);
							
							loadArticle(articleUrl, imgUrl, termTaxonomy, operationUserId);
							WebSitUtil.wating3();
						}
					}
				}
			}
		}catch(Exception e) {
			log.error(e.getMessage(), e);
		}
	}
}
